<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one or more</a>
<span class="sourceLineNo">003</span><a id="line.3"> * contributor license agreements.  See the NOTICE file distributed with</a>
<span class="sourceLineNo">004</span><a id="line.4"> * this work for additional information regarding copyright ownership.</a>
<span class="sourceLineNo">005</span><a id="line.5"> * The ASF licenses this file to You under the Apache License, Version 2.0</a>
<span class="sourceLineNo">006</span><a id="line.6"> * (the "License"); you may not use this file except in compliance with</a>
<span class="sourceLineNo">007</span><a id="line.7"> * the License.  You may obtain a copy of the License at</a>
<span class="sourceLineNo">008</span><a id="line.8"> *</a>
<span class="sourceLineNo">009</span><a id="line.9"> *      http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">010</span><a id="line.10"> *</a>
<span class="sourceLineNo">011</span><a id="line.11"> * Unless required by applicable law or agreed to in writing, software</a>
<span class="sourceLineNo">012</span><a id="line.12"> * distributed under the License is distributed on an "AS IS" BASIS,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</a>
<span class="sourceLineNo">014</span><a id="line.14"> * See the License for the specific language governing permissions and</a>
<span class="sourceLineNo">015</span><a id="line.15"> * limitations under the License.</a>
<span class="sourceLineNo">016</span><a id="line.16"> */</a>
<span class="sourceLineNo">017</span><a id="line.17">package org.apache.commons.dbutils;</a>
<span class="sourceLineNo">018</span><a id="line.18"></a>
<span class="sourceLineNo">019</span><a id="line.19">import java.beans.IntrospectionException;</a>
<span class="sourceLineNo">020</span><a id="line.20">import java.beans.Introspector;</a>
<span class="sourceLineNo">021</span><a id="line.21">import java.beans.PropertyDescriptor;</a>
<span class="sourceLineNo">022</span><a id="line.22">import java.lang.reflect.InvocationTargetException;</a>
<span class="sourceLineNo">023</span><a id="line.23">import java.lang.reflect.Method;</a>
<span class="sourceLineNo">024</span><a id="line.24">import java.sql.CallableStatement;</a>
<span class="sourceLineNo">025</span><a id="line.25">import java.sql.Connection;</a>
<span class="sourceLineNo">026</span><a id="line.26">import java.sql.ParameterMetaData;</a>
<span class="sourceLineNo">027</span><a id="line.27">import java.sql.PreparedStatement;</a>
<span class="sourceLineNo">028</span><a id="line.28">import java.sql.ResultSet;</a>
<span class="sourceLineNo">029</span><a id="line.29">import java.sql.SQLException;</a>
<span class="sourceLineNo">030</span><a id="line.30">import java.sql.SQLFeatureNotSupportedException;</a>
<span class="sourceLineNo">031</span><a id="line.31">import java.sql.Statement;</a>
<span class="sourceLineNo">032</span><a id="line.32">import java.sql.Types;</a>
<span class="sourceLineNo">033</span><a id="line.33">import java.util.Arrays;</a>
<span class="sourceLineNo">034</span><a id="line.34"></a>
<span class="sourceLineNo">035</span><a id="line.35">import javax.sql.DataSource;</a>
<span class="sourceLineNo">036</span><a id="line.36"></a>
<span class="sourceLineNo">037</span><a id="line.37">/**</a>
<span class="sourceLineNo">038</span><a id="line.38"> * The base class for QueryRunner &amp;amp; AsyncQueryRunner. This class is thread safe.</a>
<span class="sourceLineNo">039</span><a id="line.39"> *</a>
<span class="sourceLineNo">040</span><a id="line.40"> * @since 1.4 (mostly extracted from QueryRunner)</a>
<span class="sourceLineNo">041</span><a id="line.41"> */</a>
<span class="sourceLineNo">042</span><a id="line.42">public abstract class AbstractQueryRunner {</a>
<span class="sourceLineNo">043</span><a id="line.43">    /**</a>
<span class="sourceLineNo">044</span><a id="line.44">     * Is {@link ParameterMetaData#getParameterType(int)} broken (have we tried</a>
<span class="sourceLineNo">045</span><a id="line.45">     * it yet)?</a>
<span class="sourceLineNo">046</span><a id="line.46">     */</a>
<span class="sourceLineNo">047</span><a id="line.47">    private volatile boolean pmdKnownBroken = false;</a>
<span class="sourceLineNo">048</span><a id="line.48"></a>
<span class="sourceLineNo">049</span><a id="line.49">    /**</a>
<span class="sourceLineNo">050</span><a id="line.50">     * The DataSource to retrieve connections from.</a>
<span class="sourceLineNo">051</span><a id="line.51">     * @deprecated Access to this field should be through {@link #getDataSource()}.</a>
<span class="sourceLineNo">052</span><a id="line.52">     */</a>
<span class="sourceLineNo">053</span><a id="line.53">    @Deprecated</a>
<span class="sourceLineNo">054</span><a id="line.54">    protected final DataSource ds;</a>
<span class="sourceLineNo">055</span><a id="line.55"></a>
<span class="sourceLineNo">056</span><a id="line.56">    /**</a>
<span class="sourceLineNo">057</span><a id="line.57">     * Configuration to use when preparing statements.</a>
<span class="sourceLineNo">058</span><a id="line.58">     */</a>
<span class="sourceLineNo">059</span><a id="line.59">    private final StatementConfiguration stmtConfig;</a>
<span class="sourceLineNo">060</span><a id="line.60"></a>
<span class="sourceLineNo">061</span><a id="line.61">    /**</a>
<span class="sourceLineNo">062</span><a id="line.62">     * Default constructor, sets pmdKnownBroken to false, ds to null and stmtConfig to null.</a>
<span class="sourceLineNo">063</span><a id="line.63">     */</a>
<span class="sourceLineNo">064</span><a id="line.64">    public AbstractQueryRunner() {</a>
<span class="sourceLineNo">065</span><a id="line.65">        ds = null;</a>
<span class="sourceLineNo">066</span><a id="line.66">        this.stmtConfig = null;</a>
<span class="sourceLineNo">067</span><a id="line.67">    }</a>
<span class="sourceLineNo">068</span><a id="line.68"></a>
<span class="sourceLineNo">069</span><a id="line.69">    /**</a>
<span class="sourceLineNo">070</span><a id="line.70">     * Constructor to control the use of {@code ParameterMetaData}.</a>
<span class="sourceLineNo">071</span><a id="line.71">     *</a>
<span class="sourceLineNo">072</span><a id="line.72">     * @param pmdKnownBroken</a>
<span class="sourceLineNo">073</span><a id="line.73">     *            Some drivers don't support</a>
<span class="sourceLineNo">074</span><a id="line.74">     *            {@link ParameterMetaData#getParameterType(int) }; if</a>
<span class="sourceLineNo">075</span><a id="line.75">     *            {@code pmdKnownBroken} is set to true, we won't even try</a>
<span class="sourceLineNo">076</span><a id="line.76">     *            it; if false, we'll try it, and if it breaks, we'll remember</a>
<span class="sourceLineNo">077</span><a id="line.77">     *            not to use it again.</a>
<span class="sourceLineNo">078</span><a id="line.78">     */</a>
<span class="sourceLineNo">079</span><a id="line.79">    public AbstractQueryRunner(final boolean pmdKnownBroken) {</a>
<span class="sourceLineNo">080</span><a id="line.80">        this.pmdKnownBroken = pmdKnownBroken;</a>
<span class="sourceLineNo">081</span><a id="line.81">        ds = null;</a>
<span class="sourceLineNo">082</span><a id="line.82">        this.stmtConfig = null;</a>
<span class="sourceLineNo">083</span><a id="line.83">    }</a>
<span class="sourceLineNo">084</span><a id="line.84"></a>
<span class="sourceLineNo">085</span><a id="line.85">    /**</a>
<span class="sourceLineNo">086</span><a id="line.86">     * Constructor to provide a {@code DataSource}. Methods that do not</a>
<span class="sourceLineNo">087</span><a id="line.87">     * take a {@code Connection} parameter will retrieve connections from</a>
<span class="sourceLineNo">088</span><a id="line.88">     * this {@code DataSource}.</a>
<span class="sourceLineNo">089</span><a id="line.89">     *</a>
<span class="sourceLineNo">090</span><a id="line.90">     * @param ds</a>
<span class="sourceLineNo">091</span><a id="line.91">     *            The {@code DataSource} to retrieve connections from.</a>
<span class="sourceLineNo">092</span><a id="line.92">     */</a>
<span class="sourceLineNo">093</span><a id="line.93">    public AbstractQueryRunner(final DataSource ds) {</a>
<span class="sourceLineNo">094</span><a id="line.94">        this.ds = ds;</a>
<span class="sourceLineNo">095</span><a id="line.95">        this.stmtConfig = null;</a>
<span class="sourceLineNo">096</span><a id="line.96">    }</a>
<span class="sourceLineNo">097</span><a id="line.97"></a>
<span class="sourceLineNo">098</span><a id="line.98">    /**</a>
<span class="sourceLineNo">099</span><a id="line.99">     * Constructor to provide a {@code DataSource} and control the use of</a>
<span class="sourceLineNo">100</span><a id="line.100">     * {@code ParameterMetaData}. Methods that do not take a</a>
<span class="sourceLineNo">101</span><a id="line.101">     * {@code Connection} parameter will retrieve connections from this</a>
<span class="sourceLineNo">102</span><a id="line.102">     * {@code DataSource}.</a>
<span class="sourceLineNo">103</span><a id="line.103">     *</a>
<span class="sourceLineNo">104</span><a id="line.104">     * @param ds</a>
<span class="sourceLineNo">105</span><a id="line.105">     *            The {@code DataSource} to retrieve connections from.</a>
<span class="sourceLineNo">106</span><a id="line.106">     * @param pmdKnownBroken</a>
<span class="sourceLineNo">107</span><a id="line.107">     *            Some drivers don't support</a>
<span class="sourceLineNo">108</span><a id="line.108">     *            {@link ParameterMetaData#getParameterType(int) }; if</a>
<span class="sourceLineNo">109</span><a id="line.109">     *            {@code pmdKnownBroken} is set to true, we won't even try</a>
<span class="sourceLineNo">110</span><a id="line.110">     *            it; if false, we'll try it, and if it breaks, we'll remember</a>
<span class="sourceLineNo">111</span><a id="line.111">     *            not to use it again.</a>
<span class="sourceLineNo">112</span><a id="line.112">     */</a>
<span class="sourceLineNo">113</span><a id="line.113">    public AbstractQueryRunner(final DataSource ds, final boolean pmdKnownBroken) {</a>
<span class="sourceLineNo">114</span><a id="line.114">        this.pmdKnownBroken = pmdKnownBroken;</a>
<span class="sourceLineNo">115</span><a id="line.115">        this.ds = ds;</a>
<span class="sourceLineNo">116</span><a id="line.116">        this.stmtConfig = null;</a>
<span class="sourceLineNo">117</span><a id="line.117">    }</a>
<span class="sourceLineNo">118</span><a id="line.118"></a>
<span class="sourceLineNo">119</span><a id="line.119">    /**</a>
<span class="sourceLineNo">120</span><a id="line.120">     * Constructor for QueryRunner that takes a {@code DataSource}, a {@code StatementConfiguration}, and</a>
<span class="sourceLineNo">121</span><a id="line.121">     * controls the use of {@code ParameterMetaData}.  Methods that do not take a {@code Connection} parameter</a>
<span class="sourceLineNo">122</span><a id="line.122">     * will retrieve connections from this {@code DataSource}.</a>
<span class="sourceLineNo">123</span><a id="line.123">     *</a>
<span class="sourceLineNo">124</span><a id="line.124">     * @param ds The {@code DataSource} to retrieve connections from.</a>
<span class="sourceLineNo">125</span><a id="line.125">     * @param pmdKnownBroken Some drivers don't support {@link java.sql.ParameterMetaData#getParameterType(int) };</a>
<span class="sourceLineNo">126</span><a id="line.126">     * if {@code pmdKnownBroken} is set to true, we won't even try it; if false, we'll try it,</a>
<span class="sourceLineNo">127</span><a id="line.127">     * and if it breaks, we'll remember not to use it again.</a>
<span class="sourceLineNo">128</span><a id="line.128">     * @param stmtConfig The configuration to apply to statements when they are prepared.</a>
<span class="sourceLineNo">129</span><a id="line.129">     */</a>
<span class="sourceLineNo">130</span><a id="line.130">    public AbstractQueryRunner(final DataSource ds, final boolean pmdKnownBroken, final StatementConfiguration stmtConfig) {</a>
<span class="sourceLineNo">131</span><a id="line.131">        this.pmdKnownBroken = pmdKnownBroken;</a>
<span class="sourceLineNo">132</span><a id="line.132">        this.ds = ds;</a>
<span class="sourceLineNo">133</span><a id="line.133">        this.stmtConfig = stmtConfig;</a>
<span class="sourceLineNo">134</span><a id="line.134">    }</a>
<span class="sourceLineNo">135</span><a id="line.135"></a>
<span class="sourceLineNo">136</span><a id="line.136">    /**</a>
<span class="sourceLineNo">137</span><a id="line.137">     * Constructor for QueryRunner that takes a {@code DataSource} to use and a {@code StatementConfiguration}.</a>
<span class="sourceLineNo">138</span><a id="line.138">     *</a>
<span class="sourceLineNo">139</span><a id="line.139">     * Methods that do not take a {@code Connection} parameter will retrieve connections from this</a>
<span class="sourceLineNo">140</span><a id="line.140">     * {@code DataSource}.</a>
<span class="sourceLineNo">141</span><a id="line.141">     *</a>
<span class="sourceLineNo">142</span><a id="line.142">     * @param ds The {@code DataSource} to retrieve connections from.</a>
<span class="sourceLineNo">143</span><a id="line.143">     * @param stmtConfig The configuration to apply to statements when they are prepared.</a>
<span class="sourceLineNo">144</span><a id="line.144">     */</a>
<span class="sourceLineNo">145</span><a id="line.145">    public AbstractQueryRunner(final DataSource ds, final StatementConfiguration stmtConfig) {</a>
<span class="sourceLineNo">146</span><a id="line.146">        this.ds = ds;</a>
<span class="sourceLineNo">147</span><a id="line.147">        this.stmtConfig = stmtConfig;</a>
<span class="sourceLineNo">148</span><a id="line.148">    }</a>
<span class="sourceLineNo">149</span><a id="line.149"></a>
<span class="sourceLineNo">150</span><a id="line.150">    /**</a>
<span class="sourceLineNo">151</span><a id="line.151">     * Constructor for QueryRunner that takes a {@code StatementConfiguration} to configure statements when</a>
<span class="sourceLineNo">152</span><a id="line.152">     * preparing them.</a>
<span class="sourceLineNo">153</span><a id="line.153">     *</a>
<span class="sourceLineNo">154</span><a id="line.154">     * @param stmtConfig The configuration to apply to statements when they are prepared.</a>
<span class="sourceLineNo">155</span><a id="line.155">     */</a>
<span class="sourceLineNo">156</span><a id="line.156">    public AbstractQueryRunner(final StatementConfiguration stmtConfig) {</a>
<span class="sourceLineNo">157</span><a id="line.157">        this.ds = null;</a>
<span class="sourceLineNo">158</span><a id="line.158">        this.stmtConfig = stmtConfig;</a>
<span class="sourceLineNo">159</span><a id="line.159">    }</a>
<span class="sourceLineNo">160</span><a id="line.160"></a>
<span class="sourceLineNo">161</span><a id="line.161">    /**</a>
<span class="sourceLineNo">162</span><a id="line.162">     * Close a {@code Connection}. This implementation avoids closing if</a>
<span class="sourceLineNo">163</span><a id="line.163">     * null and does &lt;strong&gt;not&lt;/strong&gt; suppress any exceptions. Subclasses</a>
<span class="sourceLineNo">164</span><a id="line.164">     * can override to provide special handling like logging.</a>
<span class="sourceLineNo">165</span><a id="line.165">     *</a>
<span class="sourceLineNo">166</span><a id="line.166">     * @param conn</a>
<span class="sourceLineNo">167</span><a id="line.167">     *            Connection to close</a>
<span class="sourceLineNo">168</span><a id="line.168">     * @throws SQLException</a>
<span class="sourceLineNo">169</span><a id="line.169">     *             if a database access error occurs</a>
<span class="sourceLineNo">170</span><a id="line.170">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">171</span><a id="line.171">     */</a>
<span class="sourceLineNo">172</span><a id="line.172">    protected void close(final Connection conn) throws SQLException {</a>
<span class="sourceLineNo">173</span><a id="line.173">        DbUtils.close(conn);</a>
<span class="sourceLineNo">174</span><a id="line.174">    }</a>
<span class="sourceLineNo">175</span><a id="line.175"></a>
<span class="sourceLineNo">176</span><a id="line.176">    /**</a>
<span class="sourceLineNo">177</span><a id="line.177">     * Close a {@code ResultSet}. This implementation avoids closing if</a>
<span class="sourceLineNo">178</span><a id="line.178">     * null and does &lt;strong&gt;not&lt;/strong&gt; suppress any exceptions. Subclasses</a>
<span class="sourceLineNo">179</span><a id="line.179">     * can override to provide special handling like logging.</a>
<span class="sourceLineNo">180</span><a id="line.180">     *</a>
<span class="sourceLineNo">181</span><a id="line.181">     * @param rs</a>
<span class="sourceLineNo">182</span><a id="line.182">     *            ResultSet to close</a>
<span class="sourceLineNo">183</span><a id="line.183">     * @throws SQLException</a>
<span class="sourceLineNo">184</span><a id="line.184">     *             if a database access error occurs</a>
<span class="sourceLineNo">185</span><a id="line.185">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">186</span><a id="line.186">     */</a>
<span class="sourceLineNo">187</span><a id="line.187">    protected void close(final ResultSet rs) throws SQLException {</a>
<span class="sourceLineNo">188</span><a id="line.188">        DbUtils.close(rs);</a>
<span class="sourceLineNo">189</span><a id="line.189">    }</a>
<span class="sourceLineNo">190</span><a id="line.190"></a>
<span class="sourceLineNo">191</span><a id="line.191">    /**</a>
<span class="sourceLineNo">192</span><a id="line.192">     * Close a {@code Statement}. This implementation avoids closing if</a>
<span class="sourceLineNo">193</span><a id="line.193">     * null and does &lt;strong&gt;not&lt;/strong&gt; suppress any exceptions. Subclasses</a>
<span class="sourceLineNo">194</span><a id="line.194">     * can override to provide special handling like logging.</a>
<span class="sourceLineNo">195</span><a id="line.195">     *</a>
<span class="sourceLineNo">196</span><a id="line.196">     * @param stmt</a>
<span class="sourceLineNo">197</span><a id="line.197">     *            Statement to close</a>
<span class="sourceLineNo">198</span><a id="line.198">     * @throws SQLException</a>
<span class="sourceLineNo">199</span><a id="line.199">     *             if a database access error occurs</a>
<span class="sourceLineNo">200</span><a id="line.200">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">201</span><a id="line.201">     */</a>
<span class="sourceLineNo">202</span><a id="line.202">    protected void close(final Statement stmt) throws SQLException {</a>
<span class="sourceLineNo">203</span><a id="line.203">        DbUtils.close(stmt);</a>
<span class="sourceLineNo">204</span><a id="line.204">    }</a>
<span class="sourceLineNo">205</span><a id="line.205"></a>
<span class="sourceLineNo">206</span><a id="line.206">    /**</a>
<span class="sourceLineNo">207</span><a id="line.207">     * Calls {@link DbUtils#closeQuietly(Connection)}.</a>
<span class="sourceLineNo">208</span><a id="line.208">     *</a>
<span class="sourceLineNo">209</span><a id="line.209">     * @param conn Connection to close.</a>
<span class="sourceLineNo">210</span><a id="line.210">     * @since 2.0</a>
<span class="sourceLineNo">211</span><a id="line.211">     */</a>
<span class="sourceLineNo">212</span><a id="line.212">    protected void closeQuietly(final Connection conn) {</a>
<span class="sourceLineNo">213</span><a id="line.213">        DbUtils.closeQuietly(conn);</a>
<span class="sourceLineNo">214</span><a id="line.214">    }</a>
<span class="sourceLineNo">215</span><a id="line.215"></a>
<span class="sourceLineNo">216</span><a id="line.216">    /**</a>
<span class="sourceLineNo">217</span><a id="line.217">     * Calls {@link DbUtils#closeQuietly(ResultSet)}.</a>
<span class="sourceLineNo">218</span><a id="line.218">     *</a>
<span class="sourceLineNo">219</span><a id="line.219">     * @param rs ResultSet to close.</a>
<span class="sourceLineNo">220</span><a id="line.220">     * @since 2.0</a>
<span class="sourceLineNo">221</span><a id="line.221">     */</a>
<span class="sourceLineNo">222</span><a id="line.222">    protected void closeQuietly(final ResultSet rs) {</a>
<span class="sourceLineNo">223</span><a id="line.223">        DbUtils.closeQuietly(rs);</a>
<span class="sourceLineNo">224</span><a id="line.224">    }</a>
<span class="sourceLineNo">225</span><a id="line.225"></a>
<span class="sourceLineNo">226</span><a id="line.226">    /**</a>
<span class="sourceLineNo">227</span><a id="line.227">     * Calls {@link DbUtils#closeQuietly(Statement)}.</a>
<span class="sourceLineNo">228</span><a id="line.228">     *</a>
<span class="sourceLineNo">229</span><a id="line.229">     * @param statement ResultSet to close.</a>
<span class="sourceLineNo">230</span><a id="line.230">     * @since 2.0</a>
<span class="sourceLineNo">231</span><a id="line.231">     */</a>
<span class="sourceLineNo">232</span><a id="line.232">    protected void closeQuietly(final Statement statement) {</a>
<span class="sourceLineNo">233</span><a id="line.233">        DbUtils.closeQuietly(statement);</a>
<span class="sourceLineNo">234</span><a id="line.234">    }</a>
<span class="sourceLineNo">235</span><a id="line.235"></a>
<span class="sourceLineNo">236</span><a id="line.236">    private void configureStatement(final Statement stmt) throws SQLException {</a>
<span class="sourceLineNo">237</span><a id="line.237"></a>
<span class="sourceLineNo">238</span><a id="line.238">        if (stmtConfig != null) {</a>
<span class="sourceLineNo">239</span><a id="line.239">            if (stmtConfig.isFetchDirectionSet()) {</a>
<span class="sourceLineNo">240</span><a id="line.240">                stmt.setFetchDirection(stmtConfig.getFetchDirection());</a>
<span class="sourceLineNo">241</span><a id="line.241">            }</a>
<span class="sourceLineNo">242</span><a id="line.242"></a>
<span class="sourceLineNo">243</span><a id="line.243">            if (stmtConfig.isFetchSizeSet()) {</a>
<span class="sourceLineNo">244</span><a id="line.244">                stmt.setFetchSize(stmtConfig.getFetchSize());</a>
<span class="sourceLineNo">245</span><a id="line.245">            }</a>
<span class="sourceLineNo">246</span><a id="line.246"></a>
<span class="sourceLineNo">247</span><a id="line.247">            if (stmtConfig.isMaxFieldSizeSet()) {</a>
<span class="sourceLineNo">248</span><a id="line.248">                stmt.setMaxFieldSize(stmtConfig.getMaxFieldSize());</a>
<span class="sourceLineNo">249</span><a id="line.249">            }</a>
<span class="sourceLineNo">250</span><a id="line.250"></a>
<span class="sourceLineNo">251</span><a id="line.251">            if (stmtConfig.isMaxRowsSet()) {</a>
<span class="sourceLineNo">252</span><a id="line.252">                stmt.setMaxRows(stmtConfig.getMaxRows());</a>
<span class="sourceLineNo">253</span><a id="line.253">            }</a>
<span class="sourceLineNo">254</span><a id="line.254"></a>
<span class="sourceLineNo">255</span><a id="line.255">            if (stmtConfig.isQueryTimeoutSet()) {</a>
<span class="sourceLineNo">256</span><a id="line.256">                stmt.setQueryTimeout(stmtConfig.getQueryTimeout());</a>
<span class="sourceLineNo">257</span><a id="line.257">            }</a>
<span class="sourceLineNo">258</span><a id="line.258">        }</a>
<span class="sourceLineNo">259</span><a id="line.259">    }</a>
<span class="sourceLineNo">260</span><a id="line.260"></a>
<span class="sourceLineNo">261</span><a id="line.261">    /**</a>
<span class="sourceLineNo">262</span><a id="line.262">     * Fill the {@code PreparedStatement} replacement parameters with the</a>
<span class="sourceLineNo">263</span><a id="line.263">     * given objects.</a>
<span class="sourceLineNo">264</span><a id="line.264">     *</a>
<span class="sourceLineNo">265</span><a id="line.265">     * @param stmt</a>
<span class="sourceLineNo">266</span><a id="line.266">     *            PreparedStatement to fill</a>
<span class="sourceLineNo">267</span><a id="line.267">     * @param params</a>
<span class="sourceLineNo">268</span><a id="line.268">     *            Query replacement parameters; {@code null} is a valid</a>
<span class="sourceLineNo">269</span><a id="line.269">     *            value to pass in.</a>
<span class="sourceLineNo">270</span><a id="line.270">     * @throws SQLException</a>
<span class="sourceLineNo">271</span><a id="line.271">     *             if a database access error occurs</a>
<span class="sourceLineNo">272</span><a id="line.272">     */</a>
<span class="sourceLineNo">273</span><a id="line.273">    public void fillStatement(final PreparedStatement stmt, final Object... params)</a>
<span class="sourceLineNo">274</span><a id="line.274">            throws SQLException {</a>
<span class="sourceLineNo">275</span><a id="line.275"></a>
<span class="sourceLineNo">276</span><a id="line.276">        // check the parameter count, if we can</a>
<span class="sourceLineNo">277</span><a id="line.277">        ParameterMetaData pmd = null;</a>
<span class="sourceLineNo">278</span><a id="line.278">        if (!pmdKnownBroken) {</a>
<span class="sourceLineNo">279</span><a id="line.279">            try {</a>
<span class="sourceLineNo">280</span><a id="line.280">                pmd = stmt.getParameterMetaData();</a>
<span class="sourceLineNo">281</span><a id="line.281">                if (pmd == null) { // can be returned by implementations that don't support the method</a>
<span class="sourceLineNo">282</span><a id="line.282">                    pmdKnownBroken = true;</a>
<span class="sourceLineNo">283</span><a id="line.283">                } else {</a>
<span class="sourceLineNo">284</span><a id="line.284">                    final int stmtCount = pmd.getParameterCount();</a>
<span class="sourceLineNo">285</span><a id="line.285">                    final int paramsCount = params == null ? 0 : params.length;</a>
<span class="sourceLineNo">286</span><a id="line.286"></a>
<span class="sourceLineNo">287</span><a id="line.287">                    if (stmtCount != paramsCount) {</a>
<span class="sourceLineNo">288</span><a id="line.288">                        throw new SQLException("Wrong number of parameters: expected "</a>
<span class="sourceLineNo">289</span><a id="line.289">                                + stmtCount + ", was given " + paramsCount);</a>
<span class="sourceLineNo">290</span><a id="line.290">                    }</a>
<span class="sourceLineNo">291</span><a id="line.291">                }</a>
<span class="sourceLineNo">292</span><a id="line.292">            } catch (final SQLFeatureNotSupportedException ex) {</a>
<span class="sourceLineNo">293</span><a id="line.293">                pmdKnownBroken = true;</a>
<span class="sourceLineNo">294</span><a id="line.294">            }</a>
<span class="sourceLineNo">295</span><a id="line.295">            // TODO see DBUTILS-117: would it make sense to catch any other SQLEx types here?</a>
<span class="sourceLineNo">296</span><a id="line.296">        }</a>
<span class="sourceLineNo">297</span><a id="line.297"></a>
<span class="sourceLineNo">298</span><a id="line.298">        // nothing to do here</a>
<span class="sourceLineNo">299</span><a id="line.299">        if (params == null) {</a>
<span class="sourceLineNo">300</span><a id="line.300">            return;</a>
<span class="sourceLineNo">301</span><a id="line.301">        }</a>
<span class="sourceLineNo">302</span><a id="line.302"></a>
<span class="sourceLineNo">303</span><a id="line.303">        CallableStatement call = null;</a>
<span class="sourceLineNo">304</span><a id="line.304">        if (stmt instanceof CallableStatement) {</a>
<span class="sourceLineNo">305</span><a id="line.305">            call = (CallableStatement) stmt;</a>
<span class="sourceLineNo">306</span><a id="line.306">        }</a>
<span class="sourceLineNo">307</span><a id="line.307"></a>
<span class="sourceLineNo">308</span><a id="line.308">        for (int i = 0; i &lt; params.length; i++) {</a>
<span class="sourceLineNo">309</span><a id="line.309">            if (params[i] != null) {</a>
<span class="sourceLineNo">310</span><a id="line.310">                if (call != null &amp;&amp; params[i] instanceof OutParameter) {</a>
<span class="sourceLineNo">311</span><a id="line.311">                    ((OutParameter)params[i]).register(call, i + 1);</a>
<span class="sourceLineNo">312</span><a id="line.312">                } else {</a>
<span class="sourceLineNo">313</span><a id="line.313">                    stmt.setObject(i + 1, params[i]);</a>
<span class="sourceLineNo">314</span><a id="line.314">                }</a>
<span class="sourceLineNo">315</span><a id="line.315">            } else {</a>
<span class="sourceLineNo">316</span><a id="line.316">                // VARCHAR works with many drivers regardless</a>
<span class="sourceLineNo">317</span><a id="line.317">                // of the actual column type. Oddly, NULL and</a>
<span class="sourceLineNo">318</span><a id="line.318">                // OTHER don't work with Oracle's drivers.</a>
<span class="sourceLineNo">319</span><a id="line.319">                int sqlType = Types.VARCHAR;</a>
<span class="sourceLineNo">320</span><a id="line.320">                if (!pmdKnownBroken) {</a>
<span class="sourceLineNo">321</span><a id="line.321">                    // TODO see DBUTILS-117: does it make sense to catch SQLEx here?</a>
<span class="sourceLineNo">322</span><a id="line.322">                    try {</a>
<span class="sourceLineNo">323</span><a id="line.323">                        /*</a>
<span class="sourceLineNo">324</span><a id="line.324">                         * It's not possible for pmdKnownBroken to change from</a>
<span class="sourceLineNo">325</span><a id="line.325">                         * true to false, (once true, always true) so pmd cannot</a>
<span class="sourceLineNo">326</span><a id="line.326">                         * be null here.</a>
<span class="sourceLineNo">327</span><a id="line.327">                         */</a>
<span class="sourceLineNo">328</span><a id="line.328">                        sqlType = pmd.getParameterType(i + 1);</a>
<span class="sourceLineNo">329</span><a id="line.329">                    } catch (final SQLException e) {</a>
<span class="sourceLineNo">330</span><a id="line.330">                        pmdKnownBroken = true;</a>
<span class="sourceLineNo">331</span><a id="line.331">                    }</a>
<span class="sourceLineNo">332</span><a id="line.332">                }</a>
<span class="sourceLineNo">333</span><a id="line.333">                stmt.setNull(i + 1, sqlType);</a>
<span class="sourceLineNo">334</span><a id="line.334">            }</a>
<span class="sourceLineNo">335</span><a id="line.335">        }</a>
<span class="sourceLineNo">336</span><a id="line.336">    }</a>
<span class="sourceLineNo">337</span><a id="line.337"></a>
<span class="sourceLineNo">338</span><a id="line.338">    /**</a>
<span class="sourceLineNo">339</span><a id="line.339">     * Fill the {@code PreparedStatement} replacement parameters with the</a>
<span class="sourceLineNo">340</span><a id="line.340">     * given object's bean property values.</a>
<span class="sourceLineNo">341</span><a id="line.341">     *</a>
<span class="sourceLineNo">342</span><a id="line.342">     * @param stmt</a>
<span class="sourceLineNo">343</span><a id="line.343">     *            PreparedStatement to fill</a>
<span class="sourceLineNo">344</span><a id="line.344">     * @param bean</a>
<span class="sourceLineNo">345</span><a id="line.345">     *            a JavaBean object</a>
<span class="sourceLineNo">346</span><a id="line.346">     * @param properties</a>
<span class="sourceLineNo">347</span><a id="line.347">     *            an ordered array of properties; this gives the order to insert</a>
<span class="sourceLineNo">348</span><a id="line.348">     *            values in the statement</a>
<span class="sourceLineNo">349</span><a id="line.349">     * @throws SQLException</a>
<span class="sourceLineNo">350</span><a id="line.350">     *             if a database access error occurs</a>
<span class="sourceLineNo">351</span><a id="line.351">     */</a>
<span class="sourceLineNo">352</span><a id="line.352">    public void fillStatementWithBean(final PreparedStatement stmt, final Object bean,</a>
<span class="sourceLineNo">353</span><a id="line.353">            final PropertyDescriptor[] properties) throws SQLException {</a>
<span class="sourceLineNo">354</span><a id="line.354">        final Object[] params = new Object[properties.length];</a>
<span class="sourceLineNo">355</span><a id="line.355">        for (int i = 0; i &lt; properties.length; i++) {</a>
<span class="sourceLineNo">356</span><a id="line.356">            final PropertyDescriptor property = properties[i];</a>
<span class="sourceLineNo">357</span><a id="line.357">            Object value = null;</a>
<span class="sourceLineNo">358</span><a id="line.358">            final Method method = property.getReadMethod();</a>
<span class="sourceLineNo">359</span><a id="line.359">            if (method == null) {</a>
<span class="sourceLineNo">360</span><a id="line.360">                throw new RuntimeException("No read method for bean property "</a>
<span class="sourceLineNo">361</span><a id="line.361">                        + bean.getClass() + " " + property.getName());</a>
<span class="sourceLineNo">362</span><a id="line.362">            }</a>
<span class="sourceLineNo">363</span><a id="line.363">            try {</a>
<span class="sourceLineNo">364</span><a id="line.364">                value = method.invoke(bean);</a>
<span class="sourceLineNo">365</span><a id="line.365">            } catch (final InvocationTargetException e) {</a>
<span class="sourceLineNo">366</span><a id="line.366">                throw new RuntimeException("Couldn't invoke method: " + method,</a>
<span class="sourceLineNo">367</span><a id="line.367">                        e);</a>
<span class="sourceLineNo">368</span><a id="line.368">            } catch (final IllegalArgumentException e) {</a>
<span class="sourceLineNo">369</span><a id="line.369">                throw new RuntimeException(</a>
<span class="sourceLineNo">370</span><a id="line.370">                        "Couldn't invoke method with 0 arguments: " + method, e);</a>
<span class="sourceLineNo">371</span><a id="line.371">            } catch (final IllegalAccessException e) {</a>
<span class="sourceLineNo">372</span><a id="line.372">                throw new RuntimeException("Couldn't invoke method: " + method,</a>
<span class="sourceLineNo">373</span><a id="line.373">                        e);</a>
<span class="sourceLineNo">374</span><a id="line.374">            }</a>
<span class="sourceLineNo">375</span><a id="line.375">            params[i] = value;</a>
<span class="sourceLineNo">376</span><a id="line.376">        }</a>
<span class="sourceLineNo">377</span><a id="line.377">        fillStatement(stmt, params);</a>
<span class="sourceLineNo">378</span><a id="line.378">    }</a>
<span class="sourceLineNo">379</span><a id="line.379"></a>
<span class="sourceLineNo">380</span><a id="line.380">    /**</a>
<span class="sourceLineNo">381</span><a id="line.381">     * Fill the {@code PreparedStatement} replacement parameters with the</a>
<span class="sourceLineNo">382</span><a id="line.382">     * given object's bean property values.</a>
<span class="sourceLineNo">383</span><a id="line.383">     *</a>
<span class="sourceLineNo">384</span><a id="line.384">     * @param stmt</a>
<span class="sourceLineNo">385</span><a id="line.385">     *            PreparedStatement to fill</a>
<span class="sourceLineNo">386</span><a id="line.386">     * @param bean</a>
<span class="sourceLineNo">387</span><a id="line.387">     *            A JavaBean object</a>
<span class="sourceLineNo">388</span><a id="line.388">     * @param propertyNames</a>
<span class="sourceLineNo">389</span><a id="line.389">     *            An ordered array of property names (these should match the</a>
<span class="sourceLineNo">390</span><a id="line.390">     *            getters/setters); this gives the order to insert values in the</a>
<span class="sourceLineNo">391</span><a id="line.391">     *            statement</a>
<span class="sourceLineNo">392</span><a id="line.392">     * @throws SQLException</a>
<span class="sourceLineNo">393</span><a id="line.393">     *             If a database access error occurs</a>
<span class="sourceLineNo">394</span><a id="line.394">     */</a>
<span class="sourceLineNo">395</span><a id="line.395">    public void fillStatementWithBean(final PreparedStatement stmt, final Object bean,</a>
<span class="sourceLineNo">396</span><a id="line.396">            final String... propertyNames) throws SQLException {</a>
<span class="sourceLineNo">397</span><a id="line.397">        PropertyDescriptor[] descriptors;</a>
<span class="sourceLineNo">398</span><a id="line.398">        try {</a>
<span class="sourceLineNo">399</span><a id="line.399">            descriptors = Introspector.getBeanInfo(bean.getClass())</a>
<span class="sourceLineNo">400</span><a id="line.400">                    .getPropertyDescriptors();</a>
<span class="sourceLineNo">401</span><a id="line.401">        } catch (final IntrospectionException e) {</a>
<span class="sourceLineNo">402</span><a id="line.402">            throw new RuntimeException("Couldn't introspect bean "</a>
<span class="sourceLineNo">403</span><a id="line.403">                    + bean.getClass().toString(), e);</a>
<span class="sourceLineNo">404</span><a id="line.404">        }</a>
<span class="sourceLineNo">405</span><a id="line.405">        final PropertyDescriptor[] sorted = new PropertyDescriptor[propertyNames.length];</a>
<span class="sourceLineNo">406</span><a id="line.406">        for (int i = 0; i &lt; propertyNames.length; i++) {</a>
<span class="sourceLineNo">407</span><a id="line.407">            final String propertyName = propertyNames[i];</a>
<span class="sourceLineNo">408</span><a id="line.408">            if (propertyName == null) {</a>
<span class="sourceLineNo">409</span><a id="line.409">                throw new NullPointerException("propertyName can't be null: "</a>
<span class="sourceLineNo">410</span><a id="line.410">                        + i);</a>
<span class="sourceLineNo">411</span><a id="line.411">            }</a>
<span class="sourceLineNo">412</span><a id="line.412">            boolean found = false;</a>
<span class="sourceLineNo">413</span><a id="line.413">            for (final PropertyDescriptor descriptor : descriptors) {</a>
<span class="sourceLineNo">414</span><a id="line.414">                if (propertyName.equals(descriptor.getName())) {</a>
<span class="sourceLineNo">415</span><a id="line.415">                    sorted[i] = descriptor;</a>
<span class="sourceLineNo">416</span><a id="line.416">                    found = true;</a>
<span class="sourceLineNo">417</span><a id="line.417">                    break;</a>
<span class="sourceLineNo">418</span><a id="line.418">                }</a>
<span class="sourceLineNo">419</span><a id="line.419">            }</a>
<span class="sourceLineNo">420</span><a id="line.420">            if (!found) {</a>
<span class="sourceLineNo">421</span><a id="line.421">                throw new RuntimeException("Couldn't find bean property: "</a>
<span class="sourceLineNo">422</span><a id="line.422">                        + bean.getClass() + " " + propertyName);</a>
<span class="sourceLineNo">423</span><a id="line.423">            }</a>
<span class="sourceLineNo">424</span><a id="line.424">        }</a>
<span class="sourceLineNo">425</span><a id="line.425">        fillStatementWithBean(stmt, bean, sorted);</a>
<span class="sourceLineNo">426</span><a id="line.426">    }</a>
<span class="sourceLineNo">427</span><a id="line.427"></a>
<span class="sourceLineNo">428</span><a id="line.428">    /**</a>
<span class="sourceLineNo">429</span><a id="line.429">     * Returns the {@code DataSource} this runner is using.</a>
<span class="sourceLineNo">430</span><a id="line.430">     * {@code QueryRunner} methods always call this method to get the</a>
<span class="sourceLineNo">431</span><a id="line.431">     * {@code DataSource} so subclasses can provide specialized behavior.</a>
<span class="sourceLineNo">432</span><a id="line.432">     *</a>
<span class="sourceLineNo">433</span><a id="line.433">     * @return DataSource the runner is using</a>
<span class="sourceLineNo">434</span><a id="line.434">     */</a>
<span class="sourceLineNo">435</span><a id="line.435">    public DataSource getDataSource() {</a>
<span class="sourceLineNo">436</span><a id="line.436">        return this.ds;</a>
<span class="sourceLineNo">437</span><a id="line.437">    }</a>
<span class="sourceLineNo">438</span><a id="line.438"></a>
<span class="sourceLineNo">439</span><a id="line.439">    /**</a>
<span class="sourceLineNo">440</span><a id="line.440">     * Some drivers don't support</a>
<span class="sourceLineNo">441</span><a id="line.441">     * {@link ParameterMetaData#getParameterType(int) }; if</a>
<span class="sourceLineNo">442</span><a id="line.442">     * {@code pmdKnownBroken} is set to true, we won't even try it; if</a>
<span class="sourceLineNo">443</span><a id="line.443">     * false, we'll try it, and if it breaks, we'll remember not to use it</a>
<span class="sourceLineNo">444</span><a id="line.444">     * again.</a>
<span class="sourceLineNo">445</span><a id="line.445">     *</a>
<span class="sourceLineNo">446</span><a id="line.446">     * @return the flag to skip (or not)</a>
<span class="sourceLineNo">447</span><a id="line.447">     *         {@link ParameterMetaData#getParameterType(int) }</a>
<span class="sourceLineNo">448</span><a id="line.448">     * @since 1.4</a>
<span class="sourceLineNo">449</span><a id="line.449">     */</a>
<span class="sourceLineNo">450</span><a id="line.450">    public boolean isPmdKnownBroken() {</a>
<span class="sourceLineNo">451</span><a id="line.451">        return pmdKnownBroken;</a>
<span class="sourceLineNo">452</span><a id="line.452">    }</a>
<span class="sourceLineNo">453</span><a id="line.453"></a>
<span class="sourceLineNo">454</span><a id="line.454">    /**</a>
<span class="sourceLineNo">455</span><a id="line.455">     * Factory method that creates and initializes a</a>
<span class="sourceLineNo">456</span><a id="line.456">     * {@code CallableStatement} object for the given SQL.</a>
<span class="sourceLineNo">457</span><a id="line.457">     * {@code QueryRunner} methods always call this method to prepare</a>
<span class="sourceLineNo">458</span><a id="line.458">     * callable statements for them. Subclasses can override this method to</a>
<span class="sourceLineNo">459</span><a id="line.459">     * provide special CallableStatement configuration if needed. This</a>
<span class="sourceLineNo">460</span><a id="line.460">     * implementation simply calls {@code conn.prepareCall(sql)}.</a>
<span class="sourceLineNo">461</span><a id="line.461">     *</a>
<span class="sourceLineNo">462</span><a id="line.462">     * @param conn</a>
<span class="sourceLineNo">463</span><a id="line.463">     *            The {@code Connection} used to create the</a>
<span class="sourceLineNo">464</span><a id="line.464">     *            {@code CallableStatement}</a>
<span class="sourceLineNo">465</span><a id="line.465">     * @param sql</a>
<span class="sourceLineNo">466</span><a id="line.466">     *            The SQL statement to prepare.</a>
<span class="sourceLineNo">467</span><a id="line.467">     * @return An initialized {@code CallableStatement}.</a>
<span class="sourceLineNo">468</span><a id="line.468">     * @throws SQLException</a>
<span class="sourceLineNo">469</span><a id="line.469">     *             if a database access error occurs</a>
<span class="sourceLineNo">470</span><a id="line.470">     */</a>
<span class="sourceLineNo">471</span><a id="line.471">    protected CallableStatement prepareCall(final Connection conn, final String sql)</a>
<span class="sourceLineNo">472</span><a id="line.472">            throws SQLException {</a>
<span class="sourceLineNo">473</span><a id="line.473"></a>
<span class="sourceLineNo">474</span><a id="line.474">        return conn.prepareCall(sql);</a>
<span class="sourceLineNo">475</span><a id="line.475">    }</a>
<span class="sourceLineNo">476</span><a id="line.476"></a>
<span class="sourceLineNo">477</span><a id="line.477">    /**</a>
<span class="sourceLineNo">478</span><a id="line.478">     * Factory method that creates and initializes a {@code Connection}</a>
<span class="sourceLineNo">479</span><a id="line.479">     * object. {@code QueryRunner} methods always call this method to</a>
<span class="sourceLineNo">480</span><a id="line.480">     * retrieve connections from its DataSource. Subclasses can override this</a>
<span class="sourceLineNo">481</span><a id="line.481">     * method to provide special {@code Connection} configuration if</a>
<span class="sourceLineNo">482</span><a id="line.482">     * needed. This implementation simply calls {@code ds.getConnection()}.</a>
<span class="sourceLineNo">483</span><a id="line.483">     *</a>
<span class="sourceLineNo">484</span><a id="line.484">     * @return An initialized {@code Connection}.</a>
<span class="sourceLineNo">485</span><a id="line.485">     * @throws SQLException</a>
<span class="sourceLineNo">486</span><a id="line.486">     *             if a database access error occurs</a>
<span class="sourceLineNo">487</span><a id="line.487">     * @since DbUtils 1.1</a>
<span class="sourceLineNo">488</span><a id="line.488">     */</a>
<span class="sourceLineNo">489</span><a id="line.489">    protected Connection prepareConnection() throws SQLException {</a>
<span class="sourceLineNo">490</span><a id="line.490">        if (this.getDataSource() == null) {</a>
<span class="sourceLineNo">491</span><a id="line.491">            throw new SQLException(</a>
<span class="sourceLineNo">492</span><a id="line.492">                    "QueryRunner requires a DataSource to be "</a>
<span class="sourceLineNo">493</span><a id="line.493">                            + "invoked in this way, or a Connection should be passed in");</a>
<span class="sourceLineNo">494</span><a id="line.494">        }</a>
<span class="sourceLineNo">495</span><a id="line.495">        return this.getDataSource().getConnection();</a>
<span class="sourceLineNo">496</span><a id="line.496">    }</a>
<span class="sourceLineNo">497</span><a id="line.497"></a>
<span class="sourceLineNo">498</span><a id="line.498">    /**</a>
<span class="sourceLineNo">499</span><a id="line.499">     * Factory method that creates and initializes a</a>
<span class="sourceLineNo">500</span><a id="line.500">     * {@code PreparedStatement} object for the given SQL.</a>
<span class="sourceLineNo">501</span><a id="line.501">     * {@code QueryRunner} methods always call this method to prepare</a>
<span class="sourceLineNo">502</span><a id="line.502">     * statements for them. Subclasses can override this method to provide</a>
<span class="sourceLineNo">503</span><a id="line.503">     * special PreparedStatement configuration if needed. This implementation</a>
<span class="sourceLineNo">504</span><a id="line.504">     * simply calls {@code conn.prepareStatement(sql)}.</a>
<span class="sourceLineNo">505</span><a id="line.505">     *</a>
<span class="sourceLineNo">506</span><a id="line.506">     * @param conn</a>
<span class="sourceLineNo">507</span><a id="line.507">     *            The {@code Connection} used to create the</a>
<span class="sourceLineNo">508</span><a id="line.508">     *            {@code PreparedStatement}</a>
<span class="sourceLineNo">509</span><a id="line.509">     * @param sql</a>
<span class="sourceLineNo">510</span><a id="line.510">     *            The SQL statement to prepare.</a>
<span class="sourceLineNo">511</span><a id="line.511">     * @return An initialized {@code PreparedStatement}.</a>
<span class="sourceLineNo">512</span><a id="line.512">     * @throws SQLException</a>
<span class="sourceLineNo">513</span><a id="line.513">     *             if a database access error occurs</a>
<span class="sourceLineNo">514</span><a id="line.514">     */</a>
<span class="sourceLineNo">515</span><a id="line.515">    protected PreparedStatement prepareStatement(final Connection conn, final String sql)</a>
<span class="sourceLineNo">516</span><a id="line.516">            throws SQLException {</a>
<span class="sourceLineNo">517</span><a id="line.517"></a>
<span class="sourceLineNo">518</span><a id="line.518">        @SuppressWarnings("resource")</a>
<span class="sourceLineNo">519</span><a id="line.519">        final</a>
<span class="sourceLineNo">520</span><a id="line.520">        PreparedStatement ps = conn.prepareStatement(sql);</a>
<span class="sourceLineNo">521</span><a id="line.521">        try {</a>
<span class="sourceLineNo">522</span><a id="line.522">            configureStatement(ps);</a>
<span class="sourceLineNo">523</span><a id="line.523">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">524</span><a id="line.524">            ps.close();</a>
<span class="sourceLineNo">525</span><a id="line.525">            throw e;</a>
<span class="sourceLineNo">526</span><a id="line.526">        }</a>
<span class="sourceLineNo">527</span><a id="line.527">        return ps;</a>
<span class="sourceLineNo">528</span><a id="line.528">    }</a>
<span class="sourceLineNo">529</span><a id="line.529"></a>
<span class="sourceLineNo">530</span><a id="line.530">    /**</a>
<span class="sourceLineNo">531</span><a id="line.531">     * Factory method that creates and initializes a</a>
<span class="sourceLineNo">532</span><a id="line.532">     * {@code PreparedStatement} object for the given SQL.</a>
<span class="sourceLineNo">533</span><a id="line.533">     * {@code QueryRunner} methods always call this method to prepare</a>
<span class="sourceLineNo">534</span><a id="line.534">     * statements for them. Subclasses can override this method to provide</a>
<span class="sourceLineNo">535</span><a id="line.535">     * special PreparedStatement configuration if needed. This implementation</a>
<span class="sourceLineNo">536</span><a id="line.536">     * simply calls {@code conn.prepareStatement(sql, returnedKeys)}</a>
<span class="sourceLineNo">537</span><a id="line.537">     * which will result in the ability to retrieve the automatically-generated</a>
<span class="sourceLineNo">538</span><a id="line.538">     * keys from an auto_increment column.</a>
<span class="sourceLineNo">539</span><a id="line.539">     *</a>
<span class="sourceLineNo">540</span><a id="line.540">     * @param conn</a>
<span class="sourceLineNo">541</span><a id="line.541">     *            The {@code Connection} used to create the</a>
<span class="sourceLineNo">542</span><a id="line.542">     *            {@code PreparedStatement}</a>
<span class="sourceLineNo">543</span><a id="line.543">     * @param sql</a>
<span class="sourceLineNo">544</span><a id="line.544">     *            The SQL statement to prepare.</a>
<span class="sourceLineNo">545</span><a id="line.545">     * @param returnedKeys</a>
<span class="sourceLineNo">546</span><a id="line.546">     *            Flag indicating whether to return generated keys or not.</a>
<span class="sourceLineNo">547</span><a id="line.547">     *</a>
<span class="sourceLineNo">548</span><a id="line.548">     * @return An initialized {@code PreparedStatement}.</a>
<span class="sourceLineNo">549</span><a id="line.549">     * @throws SQLException</a>
<span class="sourceLineNo">550</span><a id="line.550">     *             if a database access error occurs</a>
<span class="sourceLineNo">551</span><a id="line.551">     * @since 1.6</a>
<span class="sourceLineNo">552</span><a id="line.552">     */</a>
<span class="sourceLineNo">553</span><a id="line.553">    protected PreparedStatement prepareStatement(final Connection conn, final String sql, final int returnedKeys)</a>
<span class="sourceLineNo">554</span><a id="line.554">            throws SQLException {</a>
<span class="sourceLineNo">555</span><a id="line.555"></a>
<span class="sourceLineNo">556</span><a id="line.556">        @SuppressWarnings("resource")</a>
<span class="sourceLineNo">557</span><a id="line.557">        final</a>
<span class="sourceLineNo">558</span><a id="line.558">        PreparedStatement ps = conn.prepareStatement(sql, returnedKeys);</a>
<span class="sourceLineNo">559</span><a id="line.559">        try {</a>
<span class="sourceLineNo">560</span><a id="line.560">            configureStatement(ps);</a>
<span class="sourceLineNo">561</span><a id="line.561">        } catch (final SQLException e) {</a>
<span class="sourceLineNo">562</span><a id="line.562">            ps.close();</a>
<span class="sourceLineNo">563</span><a id="line.563">            throw e;</a>
<span class="sourceLineNo">564</span><a id="line.564">        }</a>
<span class="sourceLineNo">565</span><a id="line.565">        return ps;</a>
<span class="sourceLineNo">566</span><a id="line.566">    }</a>
<span class="sourceLineNo">567</span><a id="line.567"></a>
<span class="sourceLineNo">568</span><a id="line.568">    /**</a>
<span class="sourceLineNo">569</span><a id="line.569">     * Throws a new exception with a more informative error message.</a>
<span class="sourceLineNo">570</span><a id="line.570">     *</a>
<span class="sourceLineNo">571</span><a id="line.571">     * @param cause</a>
<span class="sourceLineNo">572</span><a id="line.572">     *            The original exception that will be chained to the new</a>
<span class="sourceLineNo">573</span><a id="line.573">     *            exception when it's rethrown.</a>
<span class="sourceLineNo">574</span><a id="line.574">     *</a>
<span class="sourceLineNo">575</span><a id="line.575">     * @param sql</a>
<span class="sourceLineNo">576</span><a id="line.576">     *            The query that was executing when the exception happened.</a>
<span class="sourceLineNo">577</span><a id="line.577">     *</a>
<span class="sourceLineNo">578</span><a id="line.578">     * @param params</a>
<span class="sourceLineNo">579</span><a id="line.579">     *            The query replacement parameters; {@code null} is a valid</a>
<span class="sourceLineNo">580</span><a id="line.580">     *            value to pass in.</a>
<span class="sourceLineNo">581</span><a id="line.581">     *</a>
<span class="sourceLineNo">582</span><a id="line.582">     * @throws SQLException</a>
<span class="sourceLineNo">583</span><a id="line.583">     *             if a database access error occurs</a>
<span class="sourceLineNo">584</span><a id="line.584">     */</a>
<span class="sourceLineNo">585</span><a id="line.585">    protected void rethrow(final SQLException cause, final String sql, final Object... params)</a>
<span class="sourceLineNo">586</span><a id="line.586">            throws SQLException {</a>
<span class="sourceLineNo">587</span><a id="line.587"></a>
<span class="sourceLineNo">588</span><a id="line.588">        String causeMessage = cause.getMessage();</a>
<span class="sourceLineNo">589</span><a id="line.589">        if (causeMessage == null) {</a>
<span class="sourceLineNo">590</span><a id="line.590">            causeMessage = "";</a>
<span class="sourceLineNo">591</span><a id="line.591">        }</a>
<span class="sourceLineNo">592</span><a id="line.592">        final StringBuffer msg = new StringBuffer(causeMessage);</a>
<span class="sourceLineNo">593</span><a id="line.593"></a>
<span class="sourceLineNo">594</span><a id="line.594">        msg.append(" Query: ");</a>
<span class="sourceLineNo">595</span><a id="line.595">        msg.append(sql);</a>
<span class="sourceLineNo">596</span><a id="line.596">        msg.append(" Parameters: ");</a>
<span class="sourceLineNo">597</span><a id="line.597"></a>
<span class="sourceLineNo">598</span><a id="line.598">        if (params == null) {</a>
<span class="sourceLineNo">599</span><a id="line.599">            msg.append("[]");</a>
<span class="sourceLineNo">600</span><a id="line.600">        } else {</a>
<span class="sourceLineNo">601</span><a id="line.601">            msg.append(Arrays.deepToString(params));</a>
<span class="sourceLineNo">602</span><a id="line.602">        }</a>
<span class="sourceLineNo">603</span><a id="line.603"></a>
<span class="sourceLineNo">604</span><a id="line.604">        final SQLException e = new SQLException(msg.toString(), cause.getSQLState(),</a>
<span class="sourceLineNo">605</span><a id="line.605">                cause.getErrorCode());</a>
<span class="sourceLineNo">606</span><a id="line.606">        e.setNextException(cause);</a>
<span class="sourceLineNo">607</span><a id="line.607"></a>
<span class="sourceLineNo">608</span><a id="line.608">        throw e;</a>
<span class="sourceLineNo">609</span><a id="line.609">    }</a>
<span class="sourceLineNo">610</span><a id="line.610"></a>
<span class="sourceLineNo">611</span><a id="line.611">    /**</a>
<span class="sourceLineNo">612</span><a id="line.612">     * Wrap the {@code ResultSet} in a decorator before processing it. This</a>
<span class="sourceLineNo">613</span><a id="line.613">     * implementation returns the {@code ResultSet} it is given without any</a>
<span class="sourceLineNo">614</span><a id="line.614">     * decoration.</a>
<span class="sourceLineNo">615</span><a id="line.615">     *</a>
<span class="sourceLineNo">616</span><a id="line.616">     * &lt;p&gt;</a>
<span class="sourceLineNo">617</span><a id="line.617">     * Often, the implementation of this method can be done in an anonymous</a>
<span class="sourceLineNo">618</span><a id="line.618">     * inner class like this:</a>
<span class="sourceLineNo">619</span><a id="line.619">     * &lt;/p&gt;</a>
<span class="sourceLineNo">620</span><a id="line.620">     *</a>
<span class="sourceLineNo">621</span><a id="line.621">     * &lt;pre&gt;</a>
<span class="sourceLineNo">622</span><a id="line.622">     * QueryRunner run = new QueryRunner() {</a>
<span class="sourceLineNo">623</span><a id="line.623">     *     protected ResultSet wrap(ResultSet rs) {</a>
<span class="sourceLineNo">624</span><a id="line.624">     *         return StringTrimmedResultSet.wrap(rs);</a>
<span class="sourceLineNo">625</span><a id="line.625">     *     }</a>
<span class="sourceLineNo">626</span><a id="line.626">     * };</a>
<span class="sourceLineNo">627</span><a id="line.627">     * &lt;/pre&gt;</a>
<span class="sourceLineNo">628</span><a id="line.628">     *</a>
<span class="sourceLineNo">629</span><a id="line.629">     * @param rs</a>
<span class="sourceLineNo">630</span><a id="line.630">     *            The {@code ResultSet} to decorate; never</a>
<span class="sourceLineNo">631</span><a id="line.631">     *            {@code null}.</a>
<span class="sourceLineNo">632</span><a id="line.632">     * @return The {@code ResultSet} wrapped in some decorator.</a>
<span class="sourceLineNo">633</span><a id="line.633">     */</a>
<span class="sourceLineNo">634</span><a id="line.634">    protected ResultSet wrap(final ResultSet rs) {</a>
<span class="sourceLineNo">635</span><a id="line.635">        return rs;</a>
<span class="sourceLineNo">636</span><a id="line.636">    }</a>
<span class="sourceLineNo">637</span><a id="line.637"></a>
<span class="sourceLineNo">638</span><a id="line.638">}</a>




























































</pre>
</div>
</main>
</body>
</html>
